using System;
using System.Collections.Generic;

namespace WMSSystem.Application.Workflow
{
    /// <summary>
    /// 入库审批辅助类
    /// </summary>
    public static class InboundApprovalHelper
    {
        /// <summary>
        /// 审批步骤定义
        /// </summary>
        public static class ApprovalSteps
        {
            public const string ManagerApproval = "ManagerApproval";
            public const string FinanceApproval = "FinanceApproval";
            public const string WarehouseManagerApproval = "WarehouseManagerApproval";
        }

        /// <summary>
        /// 审批角色定义
        /// </summary>
        public static class ApprovalRoles
        {
            public const string Manager = "Manager";
            public const string Finance = "Finance";
            public const string WarehouseManager = "WarehouseManager";
        }

        /// <summary>
        /// 根据步骤名称获取审批角色
        /// </summary>
        /// <param name="stepName">步骤名称</param>
        /// <returns>审批角色</returns>
        public static string GetApprovalRole(string stepName)
        {
            return stepName switch
            {
                ApprovalSteps.ManagerApproval => ApprovalRoles.Manager,
                ApprovalSteps.FinanceApproval => ApprovalRoles.Finance,
                ApprovalSteps.WarehouseManagerApproval => ApprovalRoles.WarehouseManager,
                _ => throw new ArgumentException($"未知的审批步骤: {stepName}")
            };
        }

        /// <summary>
        /// 根据角色获取步骤名称
        /// </summary>
        /// <param name="role">审批角色</param>
        /// <returns>步骤名称</returns>
        public static string GetStepName(string role)
        {
            return role switch
            {
                ApprovalRoles.Manager => ApprovalSteps.ManagerApproval,
                ApprovalRoles.Finance => ApprovalSteps.FinanceApproval,
                ApprovalRoles.WarehouseManager => ApprovalSteps.WarehouseManagerApproval,
                _ => throw new ArgumentException($"未知的审批角色: {role}")
            };
        }

        /// <summary>
        /// 获取审批步骤顺序
        /// </summary>
        /// <returns>审批步骤列表</returns>
        public static List<string> GetApprovalStepOrder()
        {
            return new List<string>
            {
                ApprovalSteps.ManagerApproval,
                ApprovalSteps.FinanceApproval,
                ApprovalSteps.WarehouseManagerApproval
            };
        }

        /// <summary>
        /// 检查审批人是否有权限审批当前步骤
        /// </summary>
        /// <param name="stepName">当前步骤</param>
        /// <param name="approverId">审批人ID</param>
        /// <param name="workflowData">工作流数据</param>
        /// <returns>是否有权限</returns>
        public static bool HasApprovalPermission(string stepName, Guid approverId, InboundApprovalData workflowData)
        {
            return stepName switch
            {
                ApprovalSteps.ManagerApproval => workflowData.ManagerId == approverId,
                ApprovalSteps.FinanceApproval => workflowData.FinanceId == approverId,
                ApprovalSteps.WarehouseManagerApproval => workflowData.WarehouseManagerId == approverId,
                _ => false
            };
        }

        /// <summary>
        /// 获取下一步审批步骤
        /// </summary>
        /// <param name="currentStep">当前步骤</param>
        /// <returns>下一步骤</returns>
        public static string GetNextStep(string currentStep)
        {
            return currentStep switch
            {
                ApprovalSteps.ManagerApproval => ApprovalSteps.FinanceApproval,
                ApprovalSteps.FinanceApproval => ApprovalSteps.WarehouseManagerApproval,
                ApprovalSteps.WarehouseManagerApproval => null, // 最后一步
                _ => throw new ArgumentException($"未知的当前步骤: {currentStep}")
            };
        }

        /// <summary>
        /// 检查是否所有审批都已完成
        /// </summary>
        /// <param name="workflowData">工作流数据</param>
        /// <returns>是否完成</returns>
        public static bool IsAllApprovalsCompleted(InboundApprovalData workflowData)
        {
            return workflowData.ManagerApproved && 
                   workflowData.FinanceApproved && 
                   workflowData.WarehouseManagerApproved;
        }

        /// <summary>
        /// 获取审批状态描述
        /// </summary>
        /// <param name="workflowData">工作流数据</param>
        /// <returns>状态描述</returns>
        public static string GetApprovalStatusDescription(InboundApprovalData workflowData)
        {
            if (!workflowData.ManagerApproved)
                return "等待经理审批";
            
            if (!workflowData.FinanceApproved)
                return "等待财务审批";
            
            if (!workflowData.WarehouseManagerApproved)
                return "等待仓库管理员审批";
            
            return "所有审批已完成";
        }
    }
} 